import { BlackType, DirectionType, MoveKeyType } from ".";
import { Black } from "./black";
import { sprite } from "./Sprite";
import { mainMap } from "./map";
import { Bounds } from "leafer-ui";
import { tankGroup } from "./tankGroup";
const generateRandom11DigitNumber = () => {
  let min = 9999;
  let max = 1000;
  return Math.floor(Math.random() * (max - min + 1)) + min;
};
export class moveBlack extends Black {
  speed: number;
  status = true;
  id;
  constructor({
    x,
    y,
    width,
    height,
    targter,
    speed,
    direction = null,
    isRadiusPoint,
    id,
  }: BlackType & {
    speed: number;
    direction?: DirectionType | null;
    id?: string;
  }) {
    super({ x, y, width, height, targter, isRadiusPoint });
    this.speed = speed;
    this.id = sprite.add(
      id || new Date().getTime() + "" + generateRandom11DigitNumber(),
      this
    );
    this.actionDirection = direction;
  }
  actionDirection: DirectionType | null = null;
  // 检测是否碰撞
  isCollide(code: DirectionType) {
    const tar = this.targter.getBounds("box", "local");
    const bounds = new Bounds({
      x: tar.x,
      y: tar.y,
      width: this.targter.width || 0,
      height: this.targter.height || 0,
    });
    const item: Black | undefined = (
      mainMap.getPointVicinity({
        x: bounds.x,
        y: bounds.y,
        actionDirection: code,
      }) as Black[]
    )
      .concat(tankGroup.getAllTank())
      .find((item) => {
        if (!item || !item.isRadiusPoint) return false;
        return bounds.hit(item.targter.getBounds("box", "local"));
      });
    return item;
  }
  // 移动
  move(code: DirectionType) {
    switch (code) {
      case "top":
        this.targter.move(0, -1 * this.speed);
        break;
      case "down":
        this.targter.move(0, 1 * this.speed);
        break;
      case "left":
        this.targter.move(-1 * this.speed, 0);
        break;
      case "right":
        this.targter.move(1 * this.speed, 0);
        break;
    }
  }
  draw() {
    this.status && this.actionDirection && this.move(this.actionDirection);
  }
  remove() {
    this.targter.destroy();
    this.status = false;
  }
}
